<template>
    <div class="write">
        <van-search shape="round" class="mysearch" background="#009ff8" left-icon="" v-model="value" show-action placeholder="写评论" @search="onSearch">
            <template #action>
                <van-button class="mybtn" type="primary" @click="onSearch">发送</van-button>
            </template>
        </van-search>
    </div>
</template>

<script>
import { apiPassComment, apiPassReply } from '@/api/comment.js'
export default {
  props: ['isReply', 'commid'],
  data () {
    return {
      value: ''
    }
  },
  methods: {
    async onSearch (value) {
      // 判断是否是回复操作
      if (this.isReply) {
        // 是回复操作
        const res = await apiPassReply({
          commid: this.commid.toString(),
          content: value,
          artid: this.$route.params.artId
        })
        console.log('评论回复:', res)
        const myComment = res.data.data.new_obj
        // 传到父组件
        this.$emit('replyPass', myComment)
        // 清空输入框
        this.value = ''
      } else {
      // 是评论操作
        const res = await apiPassComment({
          artid: this.$route.params.artId,
          value: value
        })
        window.console.log('添加评论:', res)
        const mycomm = res.data.data.new_obj
        this.$emit('passComment', mycomm)
        // 清空输入框
        this.value = ''
      }
      console.log('onSearch')
    }
  }
}
</script>

<style lang="less" scoped>
.write {
    position: fixed;
    bottom: 0px;
    left: 0px;
    width: 100%;
    .mybtn {
        height: 34px;
        box-sizing: border-box;
    }
}
</style>
